package com.hmusic.mymusic.controller;

import com.alibaba.fastjson.JSONObject;
import com.hmusic.mymusic.domain.ListSong;
import com.hmusic.mymusic.domain.Song;
import com.hmusic.mymusic.service.ListSongService;
import com.hmusic.mymusic.service.SongService;
import com.hmusic.mymusic.utils.Consts;
import com.hmusic.mymusic.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 歌单中的歌曲 controller层
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/listSong")
public class ListSongController {

    @Autowired
    private ListSongService listSongService;
    @Autowired
    private SongService songService;

    /**
     * 给歌单添加歌曲
     */
    @PostMapping("/add")
    public Object addListSong(@RequestBody ListSong listSong){
        JSONObject jsonObject = new JSONObject();
        ListSong listSong1 = listSongService.selectByTwoId(listSong);
        if (listSong1 != null){
            jsonObject.put(Consts.CODE,3);
            jsonObject.put(Consts.MSG,"歌单歌单中已有重复歌曲");
            return jsonObject;
        }
        try{
            listSongService.insert(listSong);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"歌单添加歌曲成功");
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"歌单添加歌曲失败");
            return jsonObject;
        }
    }

    /**
     * 根据歌单id查询歌曲
     */
    @GetMapping("/detail")
    @ResponseBody
    public Object songOfSongListId(String song_list_id){
        List<ListSong> listSong = listSongService.listSongOfSongListId(song_list_id);
        List<Song> songs = new ArrayList<>();
        for (int i=0;i<listSong.size();i++){
            Song song = songService.selectByPrimaryKey(listSong.get(i).getSong_id());
            songs.add(song);
        }
        return songs;
    }

    /**
     * 修改歌单中的歌曲
     */
    @PostMapping("/update")
    public Object updateListSong(@RequestBody ListSong listSong){
        JSONObject jsonObject = new JSONObject();
        try{
            listSongService.update(listSong);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"歌单修改歌曲成功");
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"歌单修改歌曲失败");
            return jsonObject;
        }
    }

    /**
     * 删除歌单中的歌曲
     */
    @PostMapping("/delete")
    public Object deleteListSong(@RequestBody ListSong listSong){
        JSONObject jsonObject = new JSONObject();
        try{
            listSongService.delete(listSong);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"删除歌单歌曲成功");
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"删除歌单歌曲失败");
            return jsonObject;
        }
    }

}
